package framework

import (
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"kandy/utils"
	"os"
)

var DB *sql.DB

func InitDB() {
	conf := new(Conf).GetConf("")
	mysqlConf := conf.Mysql
	dsn := mysqlConf.Username + ":" + mysqlConf.Password + "@tcp(" + mysqlConf.Host + ":" + mysqlConf.Port + ")/" + mysqlConf.Database + "?charset=" + mysqlConf.Charset
	fmt.Println(dsn)

	db, err := sql.Open("mysql", dsn)
	utils.CheckErr(err)
	DB = db
	DB.SetConnMaxLifetime(100)
	DB.SetMaxOpenConns(10)

	if err := DB.Ping(); err != nil {
		fmt.Println("database connect error",err)
		os.Exit(1)
	}
	fmt.Println("database connect success")
}

func CreateTable() {
	//userTable := "CREATE TABLE IF NOT EXISTS `user`(" +
	//	"`id` INT UNSIGNED AUTO_INCREMENT," +
	//	"`username` VARCHAR(20) NOT NULL," +
	//	"`password` VARCHAR(40) NOT NULL," +
	//	"`create_time` DATETIME," +
	//	"PRIMARY KEY ( `id` )" +
	//	")ENGINE=InnoDB DEFAULT CHARSET=utf8;"
	//fmt.Println(userTable)
}
